package api_sys

import (
	"github.com/gin-gonic/gin"
	"strconv"
	"wrblog-api-go/app/common/token"
	"wrblog-api-go/app/model/model_sys"
	"wrblog-api-go/app/service/service_sys"
	"wrblog-api-go/pkg/request"
	"wrblog-api-go/pkg/result"
	"wrblog-api-go/pkg/status"
)

// @Tags  System - 字典类型管理
// @Summary  获取字典类型列表
// @Accept json
// @Produce json
// @Param Authorization header string true "Authorization"
// @Param sysDictType query model_sys.SelectSysDictType true "sysDictType"
// @Success 200 {object} result.Result "OK"
// @Router /sysDict/type/page [get]
func ApiDictTypePageList(c *gin.Context) {
	raw, _ := c.Get(request.BindObj)
	sysDictType := raw.(model_sys.SelectSysDictType)
	result.PageRes(service_sys.GetDictTypePageList(sysDictType)).Json(c)
}

// @Tags  System - 字典类型管理
// @Summary  获取字典类型详情
// @Accept json
// @Produce json
// @Param Authorization header string true "Authorization"
// @Param dictId path int true "dictId"
// @Success 200 {object} result.Result "OK"
// @Router /sysDict/type/{dictId} [get]
func ApiDictTypeById(c *gin.Context) {
	dictId, err := strconv.Atoi(c.Param("dictId"))
	if err != nil {
		result.Wrap(status.BadRequest, "参数读取失败").Json(c)
	} else {
		result.Res(service_sys.GetDictTypeById(dictId)).Json(c)
	}
}

// @Tags  System - 字典类型管理
// @Summary  添加字典类型
// @Accept json
// @Produce json
// @Param Authorization header string true "Authorization"
// @Param sysDictType body model_sys.SysDictType true "sysDictType"
// @Success 200 {object} result.Result "OK"
// @Router /sysDict/type/add [post]
func ApiAddDictType(c *gin.Context) {
	raw, _ := c.Get(request.BindObj)
	sysDictType := raw.(model_sys.SysDictTypePo)
	sysDictType.CreateBy = token.GetUk(c)
	result.Res(service_sys.AddDictType(sysDictType)).Json(c)
}

// @Tags  System - 字典类型管理
// @Summary  修改字典类型
// @Accept json
// @Produce json
// @Param Authorization header string true "Authorization"
// @Param sysDictType body model_sys.SysDictType true "sysDictType"
// @Success 200 {object} result.Result "OK"
// @Router /sysDict/type/edit [put]
func ApiEditDictType(c *gin.Context) {
	raw, _ := c.Get(request.BindObj)
	sysDictType := raw.(model_sys.SysDictTypePo)
	sysDictType.UpdateBy = token.GetUk(c)
	result.Res(service_sys.EditDictType(sysDictType)).Json(c)
}

// @Tags  System - 字典类型管理
// @Summary  删除字典类型[逻辑删除]
// @Accept json
// @Produce json
// @Param Authorization header string true "Authorization"
// @Param dictIds query string true "dictIds"
// @Success 200 {object} result.Result "OK"
// @Router /sysDict/type/removes [delete]
func ApiRemoveDictType(c *gin.Context) {
	dictIds := c.Query("dictIds")
	result.Res(service_sys.RemoveDictType(dictIds, token.GetUk(c))).Json(c)
}

// @Tags  System - 字典类型管理
// @Summary  删除缓存
// @Accept json
// @Produce json
// @Param Authorization header string true "Authorization"
// @Success 200 {object} result.Result "OK"
// @Router /sysDict/data/removeCache [delete]
func ApiRemoveCacheAll(c *gin.Context) {
	result.Ok(service_sys.RemoveCacheAll()).Json(c)
}
